/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hcsc.datapower.CMDB.gui;

import com.hcsc.datapower.CMDB.service.ConfigurationWindowController;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import org.springframework.beans.factory.annotation.Autowired;


/**
 *
 * @author I307211
 */
public class ConfigurationWindow extends JDialog {
    
    private static final String PLATFORM_DEFAULT = "Environment";
    private static final String DOMAIN_DEFAULT = "Domains";
    private static final String SERVICE_DEFAULT = "Services";
    
    private static String PLATFORM_CURRENT = "";
    private static String DOMAIN_CURRENT = "";
    private static String SERVICE_CURRENT = "";
    
    private ComboBoxModel env_model;
    private ComboBoxModel domain_model;
    private ComboBoxModel services_model;

    private ConfigurationWindowController configurationwindowcontroller;
    /**
     * Creates new form ConfigWindow
     */
    public ConfigurationWindow() {
        initComponents();        
        env_model = new DefaultComboBoxModel();
        env_model.setSelectedItem(PLATFORM_DEFAULT);
        domain_model = new DefaultComboBoxModel();
        domain_model.setSelectedItem(DOMAIN_DEFAULT);
        services_model = new DefaultComboBoxModel();
        services_model.setSelectedItem(SERVICE_DEFAULT);
        
        setEnvironmentModel(env_model);
        setDomainModel(domain_model);
        setServiceModel(services_model);
        
        //center the code
        setLocationRelativeTo(null);
        
        //display admin tools if correct Authority
        //setAdmin(AuthUser.hasAdminAccess());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jComboBox_environment = new javax.swing.JComboBox();
        jPanel_domainInfo = new javax.swing.JPanel();
        jComboBox_domains = new javax.swing.JComboBox();
        jButton1_copydomain = new javax.swing.JButton();
        jButton2_newdomain = new javax.swing.JButton();
        jButton3_deletedomain = new javax.swing.JButton();
        jPanel_serviceInfo = new javax.swing.JPanel();
        jComboBox_serviceInfo = new javax.swing.JComboBox();
        jButton1_viewservice = new javax.swing.JButton();
        jButton1_newservice = new javax.swing.JButton();
        jButton_close = new javax.swing.JButton();

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Environment"));

        jComboBox_environment.setSelectedItem(jComboBox_environment);
        jComboBox_environment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_environmentActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jComboBox_environment, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 128, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jComboBox_environment, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 20, Short.MAX_VALUE))
        );

        jPanel_domainInfo.setBorder(javax.swing.BorderFactory.createTitledBorder("Domain"));

        jComboBox_domains.setEnabled(false);
        jComboBox_domains.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_domainsActionPerformed(evt);
            }
        });

        jButton1_copydomain.setText("Copy");
        jButton1_copydomain.setEnabled(false);

        jButton2_newdomain.setText("New");
        jButton2_newdomain.setEnabled(false);

        jButton3_deletedomain.setText("Delete");
        jButton3_deletedomain.setEnabled(false);

        org.jdesktop.layout.GroupLayout jPanel_domainInfoLayout = new org.jdesktop.layout.GroupLayout(jPanel_domainInfo);
        jPanel_domainInfo.setLayout(jPanel_domainInfoLayout);
        jPanel_domainInfoLayout.setHorizontalGroup(
            jPanel_domainInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel_domainInfoLayout.createSequentialGroup()
                .addContainerGap()
                .add(jComboBox_domains, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton1_copydomain)
                .add(18, 18, 18)
                .add(jButton2_newdomain)
                .add(18, 18, 18)
                .add(jButton3_deletedomain)
                .addContainerGap(28, Short.MAX_VALUE))
        );
        jPanel_domainInfoLayout.setVerticalGroup(
            jPanel_domainInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel_domainInfoLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel_domainInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboBox_domains, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton1_copydomain)
                    .add(jButton2_newdomain)
                    .add(jButton3_deletedomain))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jPanel_serviceInfo.setBorder(javax.swing.BorderFactory.createTitledBorder("Service Information"));

        jComboBox_serviceInfo.setEnabled(false);
        jComboBox_serviceInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_serviceInfoActionPerformed(evt);
            }
        });

        jButton1_viewservice.setText("View");
        jButton1_viewservice.setEnabled(false);
        jButton1_viewservice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1_viewserviceActionPerformed(evt);
            }
        });

        jButton1_newservice.setText("New");
        jButton1_newservice.setEnabled(false);

        org.jdesktop.layout.GroupLayout jPanel_serviceInfoLayout = new org.jdesktop.layout.GroupLayout(jPanel_serviceInfo);
        jPanel_serviceInfo.setLayout(jPanel_serviceInfoLayout);
        jPanel_serviceInfoLayout.setHorizontalGroup(
            jPanel_serviceInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel_serviceInfoLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel_serviceInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel_serviceInfoLayout.createSequentialGroup()
                        .add(jComboBox_serviceInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton1_newservice))
                    .add(jButton1_viewservice, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel_serviceInfoLayout.setVerticalGroup(
            jPanel_serviceInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel_serviceInfoLayout.createSequentialGroup()
                .add(jPanel_serviceInfoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboBox_serviceInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton1_newservice))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 7, Short.MAX_VALUE)
                .add(jButton1_viewservice)
                .addContainerGap())
        );

        jButton_close.setText("Close");
        jButton_close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_closeActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel_domainInfo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel_serviceInfo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .add(0, 0, Short.MAX_VALUE)
                        .add(jButton_close)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel_domainInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel_serviceInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton_close))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_closeActionPerformed
        dispose();
    }//GEN-LAST:event_jButton_closeActionPerformed

    private void jComboBox_environmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_environmentActionPerformed
        //call controller to load domains
        String env = jComboBox_environment.getSelectedItem().toString();
        System.out.println("Selected : "  + env);
         getConfigurationwindowcontroller().loadDomains(env);
    }//GEN-LAST:event_jComboBox_environmentActionPerformed

    private void jComboBox_domainsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_domainsActionPerformed
        //call controller to load domains
        String platform = jComboBox_environment.getSelectedItem().toString().trim();
        if(jComboBox_domains.getSelectedItem()==null) return;
        String domain = jComboBox_domains.getSelectedItem().toString().trim();

        System.out.println("Selected : [ENV: " + platform + " ] [ Domain: " + domain + " ]");
        getConfigurationwindowcontroller().loadServices(platform, domain);
    }//GEN-LAST:event_jComboBox_domainsActionPerformed

    private void jComboBox_serviceInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_serviceInfoActionPerformed
        if(jComboBox_serviceInfo.getSelectedItem()==null || jComboBox_domains.getSelectedItem()==null) return;
        
        String service = jComboBox_serviceInfo.getSelectedItem().toString().trim();
        String platform = jComboBox_environment.getSelectedItem().toString().trim();
        String domain = jComboBox_domains.getSelectedItem().toString().trim();
        System.out.println("Selected : [ENV: " + platform + " ] [ Domain: " + domain + " ]");
        System.out.println("Loading : [Service: " + service + " ]");
        
        
        //enable view button
        if(service.equalsIgnoreCase("No Services")){
            jButton1_viewservice.setEnabled(false);
        }else{
            jButton1_viewservice.setEnabled(true);
        }
    }//GEN-LAST:event_jComboBox_serviceInfoActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        //load platforms
        getConfigurationwindowcontroller().loadPlatforms();
    }//GEN-LAST:event_formWindowOpened

    private void jButton1_viewserviceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1_viewserviceActionPerformed
        // TODO add your handling code here:
        
        String service = jComboBox_serviceInfo.getSelectedItem().toString().trim();
        String platform = jComboBox_environment.getSelectedItem().toString().trim();
        String domain = jComboBox_domains.getSelectedItem().toString().trim();
        System.out.println("Selected : [ENV: " + platform + " ] [ Domain: " + domain + " ]");
        System.out.println("Loading : [Service: " + service + " ]");
        
    }//GEN-LAST:event_jButton1_viewserviceActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1_copydomain;
    private javax.swing.JButton jButton1_newservice;
    private javax.swing.JButton jButton1_viewservice;
    private javax.swing.JButton jButton2_newdomain;
    private javax.swing.JButton jButton3_deletedomain;
    private javax.swing.JButton jButton_close;
    private javax.swing.JComboBox jComboBox_domains;
    private javax.swing.JComboBox jComboBox_environment;
    private javax.swing.JComboBox jComboBox_serviceInfo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel_domainInfo;
    private javax.swing.JPanel jPanel_serviceInfo;
    // End of variables declaration//GEN-END:variables
    private void setEnvironmentModel(ComboBoxModel model){
        jComboBox_environment.setModel(model);
    }
    private void setDomainModel(ComboBoxModel model){
        jComboBox_domains.setModel(model);
    }
    private void setServiceModel(ComboBoxModel model){
        jComboBox_serviceInfo.setModel(model);
    }
    
    public void setPlatforms(String platform){
        jComboBox_environment.addItem(platform);
    }
    public void setDomains(String domain){
        if(!jComboBox_domains.isEnabled())
            jComboBox_domains.setEnabled(true);
        jComboBox_domains.addItem(domain);
    }
    public void setServices(String service){
        if(!jComboBox_serviceInfo.isEnabled())
            jComboBox_serviceInfo.setEnabled(true);
        jComboBox_serviceInfo.addItem(service);
    }
    public void clearDomains(){
        jComboBox_domains.removeAllItems();
    }
    public void clearServices(){
        jComboBox_serviceInfo.removeAllItems();
    }

    public ConfigurationWindowController getConfigurationwindowcontroller() {
        return configurationwindowcontroller;
    }
    
    @Autowired
    public void setConfigurationwindowcontroller(ConfigurationWindowController configurationwindowcontroller) {
        this.configurationwindowcontroller = configurationwindowcontroller;
    }
    
    
}
